package cn.hiapi.shop.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicCurdController;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.basic.token.enums.TokenType;
import cn.hiapi.finance.api.query.FinanceBankQuery;
import cn.hiapi.finance.api.vo.FinanceBankVo;
import cn.hiapi.finance.entity.FinanceBank;
import cn.hiapi.finance.service.FinanceBankService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.finance.IBankCodeListService;
import cn.hiapi.interfaces.finance.model.BankCode;
import cn.hiapi.shop.query.ShopBankQuery;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.shop.vo.ShopBankVo;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.utils.RegexUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author AdinZ
 * @date 2023/2/17 23:21
 */
@RestController()
@RequestMapping("/shop/bank")
@Api(tags = "商家端")
@Secured(TokenType.ROLE_USER)
public class ShopBankController extends BasicCurdController<FinanceBank, Long, ShopBankVo, ShopBankQuery> {
    public ShopBankController(FinanceBankService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }

    @Override
    public FinanceBank parseData(ShopBankVo data) {
        if (data.getType() == null) throw new BasicException("请选择类型");

        if (StringUtils.contains(data.getIdCard(), "*"))
            throw new BasicException("卡号错误");
        if (StringUtils.isEmpty(data.getIdCard())) {
            throw new BasicException("请输入正确的卡号");
        }
        if (StringUtils.isEmpty(data.getBankName()) || StringUtils.isEmpty(data.getBankCode())) {
            throw new BasicException("请选择正确的开户行");
        }
        if (StringUtils.isEmpty(data.getName())) {
            throw new BasicException("请输入正确的开户人");
        }
        Token<Object> token = TokenGet.get();
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), data.getShopId());
        FinanceBank bank = data.copy(new FinanceBank());
        if (bank.getId() != null) {
            FinanceBank bank1 = basicService.get(bank.getId());
            if (bank1 != null) {
                if (bank1.getFid() != data.getShopId())
                    throw new BasicException("id错误");
            }
        }
        bank.setFid(data.getShopId(), AppConst.FID_TYPE_SHOP);
        return bank;
    }


    @Override
    public ShopBankQuery setQuery(ShopBankQuery query) {
        Token<Object> token = TokenGet.get();
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), query.getShopId());
        return super.setQuery(query);
    }

    @Override
    public List<ShopBankVo> toListVo(List<FinanceBank> list, String[] fields) {
        return this.toList(list, fields);
    }

    @Override
    public ShopBankVo toData(FinanceBank data, String[] fields) {
        ShopBankVo vo = data.copy(new ShopBankVo());
        vo.setIdCard(RegexUtils.hiddenIdCard(vo.getIdCard()));
        BankCode code = this.dispatchContext.getServiceOne(IBankCodeListService.class).findByCode(data.getBankCode());
        if (code != null)
            vo.setBankLogo(code.getLogo());
        return vo;
    }
    @Override
    public ResponseEntity deleteData(Long[] id) {
        if(id==null)return toError("请传入银行卡id");
        if(id.length!=1)return toError("请传入1个银行卡id");
        Token<Object> token = TokenGet.get();
        List<FinanceBank> banks = basicService.findList(id);
        banks.forEach(e-> this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), e.getFid()));
        return super.deleteData(id);
    }


    @ApiOperation(value = "银行列表",notes = "银行列表",nickname = "AdinZ" )
    @GetMapping("/codes")
    public ResponseEntity<List> bankCode(){
        List<BankCode> codes = this.dispatchContext.getServiceOne(IBankCodeListService.class).findAll();
        return toSuccess(codes);
    }



}
